﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Common.Mysql
{
    public class MapleMySqlManager
    {

        //public static MapleMySqlManager m_Ins;

        private MySqlConnection m_mySqlCon;

        public MapleMySqlManager(string ServerIp, string User, string Password, string Database)
        {
            string M_str_sqlcon = "server=" + ServerIp + ";user id=" + User + ";password=" + Password + ";database=" + Database; //根据自己的设置
            m_mySqlCon = new MySqlConnection(M_str_sqlcon);
            try
            {
                m_mySqlCon.Open();
                m_mySqlCon.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("数据库连接错误:" + e);
            }
        }

        public MapleMySqlManager(string M_str_sqlcon)
        {
            m_mySqlCon = new MySqlConnection(M_str_sqlcon);
            try
            {
                m_mySqlCon.Open();
                m_mySqlCon.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("数据库连接错误:" + e);
            }
        }

        /// <summary>
        /// 打开数据库
        /// </summary>
        public void Open()
        {
            m_mySqlCon.Open();
        }

        public void Dispose()
        {
            m_mySqlCon.Dispose();
        }

        /// <summary>
        /// 关闭数据库
        /// </summary>
        public void Close()
        {
            m_mySqlCon.Close();            
        }

        /// <summary>
        /// 连接状态
        /// </summary>
        /// <returns></returns>
        public ConnectionState State
        {
            get
            {
                return m_mySqlCon.State;
            }
        }
       

        #region  执行MySqlCommand命令
        /// <summary>
        /// 执行MySqlCommand
        /// </summary>
        /// <param name="M_str_sqlstr">SQL语句</param>
        public void GetMysqlCom(string M_str_sqlstr)
        {
            try
            {
                MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, m_mySqlCon);
                m_mySqlCon.Close();
                m_mySqlCon.Open();
                mysqlcom.ExecuteNonQuery();
                mysqlcom.Dispose();
                m_mySqlCon.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("执行SQL语句错误:" + e);
            }
        }
        #endregion

        #region  创建MySqlDataReader对象
        /// <summary>
        /// 创建一个MySqlDataReader对象
        /// </summary>
        /// <param name="M_str_sqlstr">SQL语句</param>
        /// <returns>返回MySqlDataReader对象</returns>
        public MySqlDataReader GetMysqlRead(string M_str_sqlstr)
        {
            try
            {
                m_mySqlCon.Close();
                m_mySqlCon.Open();
                MySqlCommand mysqlcom = new MySqlCommand(M_str_sqlstr, m_mySqlCon);
                MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection);
                //m_mySqlCon.Close();
                return mysqlread;
            }
            catch (Exception e)
            {
                Console.WriteLine("读取SQL错误:" + e);
                return null;
            }
        }
        #endregion

    }
}
